package cn.enited.devtools.apiaggregator.dto;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Accessors(chain = true)
public class ListActionResult<T> extends ActionResult {
    private static final long serialVersionUID = -9020816696072757056L;

    private List<T> data;

    public ListActionResult() {
        this.data = new ArrayList<>();
    }

    public ListActionResult(int code, String message) {
        super(code, message);
        this.data = new ArrayList<>();
    }

    private ListActionResult(int code, String message, List<T> data) {
        super(code, message);
        this.data = Objects.isNull(data) ? new ArrayList<>() : data;
    }

    public void add(T item) {
        this.data.add(item);
    }

    public void addAll(Collection<T> items) {
        this.data.addAll(items);
    }

    public void clear() {
        this.data.clear();
    }

    public static <T> ListActionResult<T> of(int code, String message, List<T> data) {
        return new ListActionResult<T>(code, message, data);
    }
}
